package ca.badenhorst.ourtrition.model;

import android.util.Log;

import ca.badenhorst.ourtrition.NutriApplication;

//import ca.badenhorst.nutrition.NutriApplication;

/**
 * Created with IntelliJ IDEA.
 * User: wade
 * Date: 17/02/14
 * Time: 11:26 AM
 * To change this template use File | Settings | File Templates.
 */
public class Nutrition implements Comparable<Nutrition> {
    private static final String TAG = Nutrition.class.getName();

    public String getNdbNo() {
        return ndbNo;
    }

    public void setNdbNo(String ndbNo) {
        this.ndbNo = ndbNo;
    }

    String ndbNo;
    String nutrNo;
    String description;
    String tagname;
    double value;
    String units;   // g,kcal,mg,kJ,µg,IU

    public String getNutrNo() {
        return nutrNo;
    }

    public void setNutrNo(String nutrNo) {
        this.nutrNo = nutrNo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTagname() {
        return tagname;
    }

    public void setTagname(String tagname) {
        this.tagname = tagname;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public String getValueAsString() {
        return this.getValue() + " " + this.getUnits();
    }

    @Override
    public int compareTo(Nutrition another) {
        return this.getTagname().compareTo(another.getTagname());
    }

    public String getValueAsStringWithWeight(double weightInGrams) {
        return getValueAsStringWithWeight(weightInGrams, 2);
    }

    public String getValueAsStringWithWeight(double weightInGrams, int numDecimalPlaces) {
        double s = this.getValue() * (weightInGrams / 100);
        return String.format("%."+numDecimalPlaces+"f", s) + " " + this.getUnits();
    }

    public String getValueAsStringWithWeightWithDailyPercentage(NutriApplication app, double weightGrams) {
        double dv = app.getValues().getDVValue(getTagname(), getUnits());
        if (dv > 0) {
            StringBuilder value = new StringBuilder(getValueAsStringWithWeight(weightGrams));
            double s = this.getValue() * (weightGrams / 100);
            double newSize = Math.round(s * 100.0) / 100.0; // rounding to two decimal places

            double per = newSize / dv;
            double newPer = Math.round(per * 10000.0) / 100.0; // rounding to two decimal places
            value.append("/").append(newPer).append("%");
            return value.toString();
        } else {
            return getValueAsStringWithWeight(weightGrams);
        }
    }

    @Override
    public String toString() {
        return "Nutrition{" +
                "ndbNo='" + ndbNo + '\'' +
                ", nutrNo='" + nutrNo + '\'' +
                ", description='" + description + '\'' +
                ", tagname='" + tagname + '\'' +
                ", value=" + value +
                ", units='" + units + '\'' +
                '}';
    }

    public void add(Nutrition nutrition) {
        if (!units.equals(nutrition)) {
            Log.e(TAG, "unmatched nutritional units cannot add - ignoring " + nutrition.getDescription());
        } else {
            value += nutrition.getValue();
        }
    }
}
